﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;


namespace tmAsk.Data
{
    public class Security
    {
        private string _QueryStringKey = "BXW_GDR_";

        public string Decrypt(string pToDecrypt, string sKey)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            byte[] buffer = new byte[pToDecrypt.Length / 2];
            for (int i = 0; i < (pToDecrypt.Length / 2); i++)
            {
                int num2 = Convert.ToInt32(pToDecrypt.Substring(i * 2, 2), 0x10);
                buffer[i] = (byte)num2;
            }
            provider.Key = Encoding.ASCII.GetBytes(sKey);
            provider.IV = Encoding.ASCII.GetBytes(sKey);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            new StringBuilder();
            return Encoding.Default.GetString(stream.ToArray());
        }

        public string DecryptQueryString(string QueryString)
        {
            return this.Decrypt(QueryString, this._QueryStringKey);
        }

        public string Encrypt(string pToEncrypt, string sKey)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            byte[] bytes = Encoding.Default.GetBytes(pToEncrypt);
            provider.Key = Encoding.ASCII.GetBytes(sKey);
            provider.IV = Encoding.ASCII.GetBytes(sKey);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            StringBuilder builder = new StringBuilder();
            foreach (byte num3 in stream.ToArray())
            {
                builder.AppendFormat("{0:X2}", num3);
            }
            builder.ToString();
            return builder.ToString();
        }

        public string EncryptQueryString(string QueryString)
        {
            return this.Encrypt(QueryString, this._QueryStringKey);
        }

        public bool ValidateString(string EnString, string FoString, int Mode)
        {
            if (Mode == 1)
            {
            }
            return (this.Decrypt(EnString, this._QueryStringKey) == FoString.ToString());
        }
    }

}